package com.carbeauty.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.carbeauty.model.CarColor;

public class CarColorDaoImpl extends HibernateDaoSupport {
	
	public List<CarColor> getCarColorByUserId(String userId) {
		System.out.println("reowruewor==="+userId);
		return getHibernateTemplate().find(
                "from CarColor u where u.userId = '"+userId+"' ");
	}
	
	public List<CarColor> getAllCarColor() {
		return getHibernateTemplate().find(
                "from CarColor ");
	}
	
	public List<CarColor> getCarColorById(String id){

		return getHibernateTemplate().find("from CarColor c where c.id= "+id);
	}
	
	public CarColor saveCarColorObj(CarColor entity){
		
		getHibernateTemplate().saveOrUpdate(entity);
		getHibernateTemplate().flush();
		return entity;
		
	}
	
	public Object executeMySQL(final String sql){
		System.out.println(sql); 
		
		return getHibernateTemplate().execute( new HibernateCallback(){ 
			public Object doInHibernate(Session session) throws HibernateException, SQLException 
			{
				Connection CurConn = session.connection();
				PreparedStatement ps = CurConn.prepareStatement(sql);
				ps.execute();
				ps.close();
				session.flush();
				return null; 
				}  
			} ); 
		
	}  
	
}

